﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void MapWAPROF(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveWAPROF(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryWAPROF(dwData, 2);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }

            MapWAPROF(ref dwLocal, dwData);

            if (!flgExist)
            {
                InsertWAPROF(dwLocal);
            }
            UpdateWAPROF(dwLocal);
        }
        public void InsertWAPROF(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "INSERT INTO WAPROF(WAPMANDANT, WAPKUNDNR, WAPWERK, WAPABT, WAPWAKZ, WAPART, WAPVAR, WAPPOS, WAPANFART, WAPBEM, WAPBIS, WAPNUMKZ, WAPME, WAPMIN, WAPMAX, WAPZUSKZ, WAPZUSATZ, WAPFILLER)"
               + " VALUES (:WAPMANDANT, '00000000', '   ', '   ', :WAPWAKZ,:WAPART, :WAPVAR, :WAPPOS, :WAPANFART,:WAPBEM, '    ', 'J', :WAPME, :WAPMIN, :WAPMAX, ' ', '          ', '                            ')";
            or1.Parameters.Add("WAPMANDANT", OracleType.NVarChar).Value = dwData["WAPMANDANT"].ToString();
            or1.Parameters.Add("WAPWAKZ", OracleType.NVarChar).Value = dwData["WAPWAKZ"].ToString();
            or1.Parameters.Add("WAPART", OracleType.NVarChar).Value = dwData["WAPART"].ToString();
            or1.Parameters.Add("WAPVAR", OracleType.NVarChar).Value = dwData["WAPVAR"].ToString();
            or1.Parameters.Add("WAPPOS", OracleType.NVarChar).Value = dwData["WAPPOS"].ToString();
            or1.Parameters.Add("WAPANFART", OracleType.NVarChar).Value = dwData["WAPANFART"].ToString();
            or1.Parameters.Add("WAPBEM", OracleType.NVarChar).Value = dwData["WAPBEM"].ToString();
            or1.Parameters.Add("WAPMIN", OracleType.NVarChar).Value = dwData["WAPMIN"].ToString();
            or1.Parameters.Add("WAPME", OracleType.NVarChar).Value = dwData["WAPME"].ToString();
            or1.Parameters.Add("WAPMAX", OracleType.NVarChar).Value = dwData["WAPMAX"].ToString();
            Connect2Proxy(or1);
        }
        public DataSet QueryWAPROF(DataRow dwData, int option = 1)
        {
            DataSet set4Return = new DataSet();
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM WAPROF WHERE  ROWNUM<2 ";
                    break;
                case 1:
                    or1.CommandText = "SELECT *FROM WAPROF WHERE   "
                    + "  WAPMANDANT=:WAPMANDANT AND WAPWAKZ=:WAPWAKZ AND WAPART=:WAPART  AND WAPVAR=:WAPVAR";
                    or1.Parameters.Add("WAPMANDANT", OracleType.NVarChar).Value = dwData["WAPMANDANT"].ToString();
                    or1.Parameters.Add("WAPWAKZ", OracleType.NVarChar).Value = dwData["WAPWAKZ"].ToString();
                    or1.Parameters.Add("WAPART", OracleType.NVarChar).Value = dwData["WAPART"].ToString();
                    or1.Parameters.Add("WAPVAR", OracleType.NVarChar).Value = dwData["WAPVAR"].ToString();
                    break;
                case 2:
                    or1.CommandText = "SELECT *FROM WAPROF WHERE   "
                    + "  WAPMANDANT=:WAPMANDANT AND WAPWAKZ=:WAPWAKZ AND WAPART=:WAPART  AND WAPVAR=:WAPVAR AND WAPANFART=:WAPANFART";
                    or1.Parameters.Add("WAPMANDANT", OracleType.NVarChar).Value = dwData["WAPMANDANT"].ToString();
                    or1.Parameters.Add("WAPWAKZ", OracleType.NVarChar).Value = dwData["WAPWAKZ"].ToString();
                    or1.Parameters.Add("WAPART", OracleType.NVarChar).Value = dwData["WAPART"].ToString();
                    or1.Parameters.Add("WAPVAR", OracleType.NVarChar).Value = dwData["WAPVAR"].ToString();
                    or1.Parameters.Add("WAPANFART", OracleType.NVarChar).Value = dwData["WAPANFART"].ToString();
                    break;
                case 5:
                    or1.CommandText = "SELECT DISTINCT LF.*,M.QPMKURZB FROM EKPOS FL JOIN WAPROF LF ON FL.EKPMANDANT=WAPMANDANT AND WAPWAKZ='F' "
                        + " AND utl_raw.cast_to_varchar2(EKPART)= LF.WAPART LEFT JOIN QPMERKM M ON M.QPMMERKM=LF.WAPANFART"
                        + " WHERE EKPSA<>'AJ' AND EKPMANDANT=:EKPMANDANT AND EKPAUFKRS=:EKPAUFKRS AND EKPAUFLFD=:EKPAUFLFD";
                    or1.Parameters.Add("EKPMANDANT", OracleType.NVarChar).Value = dwData["EKKMANDANT"].ToString();
                    or1.Parameters.Add("EKPAUFKRS", OracleType.NVarChar).Value = dwData["EKKAUFKRS"].ToString();
                    or1.Parameters.Add("EKPAUFLFD", OracleType.NVarChar).Value = dwData["EKKAUFLFD"].ToString();
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                tbLocal.TableName = "WAPROF";
            }
            set4Return.Merge(Result);
            return set4Return;
        }
        public void UpdateWAPROF(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "UPDATE WAPROF SET "
                + "WAPMANDANT=:WAPMANDANT,"
                + "WAPWAKZ=:WAPWAKZ,"
                + "WAPART=:WAPART,"
                + "WAPVAR=:WAPVAR,"
                + "WAPPOS=:WAPPOS,"
                + "WAPANFART=:WAPANFART,"
                + "WAPBEM=:WAPBEM,"
                + "WAPME=:WAPME,"
                + "WAPMIN=:WAPMIN,"
                + "WAPMAX=:WAPMAX"
                + " WHERE  "
                + "  WAPMANDANT=:WAPMANDANT AND WAPWAKZ=:WAPWAKZ  AND WAPART=:WAPART  AND WAPVAR=:WAPVAR  AND WAPANFART=:WAPANFART";

            or1.Parameters.Add("WAPMANDANT", OracleType.NVarChar).Value = dwData["WAPMANDANT"].ToString();
            or1.Parameters.Add("WAPWAKZ", OracleType.NVarChar).Value = dwData["WAPWAKZ"].ToString();
            or1.Parameters.Add("WAPART", OracleType.NVarChar).Value = dwData["WAPART"].ToString();
            or1.Parameters.Add("WAPVAR", OracleType.NVarChar).Value = dwData["WAPVAR"].ToString();
            or1.Parameters.Add("WAPPOS", OracleType.NVarChar).Value = dwData["WAPPOS"].ToString();
            or1.Parameters.Add("WAPANFART", OracleType.NVarChar).Value = dwData["WAPANFART"].ToString();
            or1.Parameters.Add("WAPBEM", OracleType.NVarChar).Value = dwData["WAPBEM"].ToString();
            or1.Parameters.Add("WAPME", OracleType.NVarChar).Value = dwData["WAPME"].ToString();
            or1.Parameters.Add("WAPMIN", OracleType.NVarChar).Value = dwData["WAPMIN"].ToString();
            or1.Parameters.Add("WAPMAX", OracleType.NVarChar).Value = dwData["WAPMAX"].ToString();
            Connect2Proxy(or1);
        }
    }
}
